package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zrrd.reggie.bean.AddressBook;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.service.AddressBookService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    //添加收货地址
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook){
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        List<AddressBook> list = addressBookService.list(wrapper);
        if ( list == null|| list.size() <= 0){
            addressBook.setIsDefault(1);
        }

        addressBookService.save(addressBook);
        return R.success("成功");
    }
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        QueryWrapper<AddressBook> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        wrapper.orderByDesc("is_default");

        //默认地址自顶
        List<AddressBook> list = addressBookService.list(wrapper);
        //
        return R.success(list);
    }
    @PutMapping("/default")
    public R<String> setdefault(@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,ThreadLocalUtil.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);
        //同一改成0
        addressBookService.update(wrapper);
        //一个默认为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }
    //这里是修改的显示方法
    @GetMapping("/{id}")
    public R<AddressBook> getAddr(@PathVariable("id") Long id){
        //好像不太需要userid
        QueryWrapper<AddressBook> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        wrapper.eq("id",id);
        AddressBook book = addressBookService.getOne(wrapper);
        return R.success(book);
    }
    //修改地址方法
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        //老师的没有下面这一行
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        addressBookService.updateById(addressBook);
        return R.success("成功");
    }
    @DeleteMapping
    public R<String> delete(Long ids){

        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        QueryWrapper<AddressBook> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        wrapper.eq("is_default",1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook !=null){
            return R.success(addressBook);
        }else {
            return R.error("没有");
        }
    }
}
